import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws Exception {
        testNumber();
        testWord();
        BufferedReader r = new BufferedReader(new FileReader("src/rersource/a.txt"));
        StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = r.readLine()) != null) {
            buffer.append(line);
        }
        r.close();
        Pattern expression = Pattern.compile("[a-zA-Z]+");// 定义正则表达式匹配单词
        String s = buffer.toString();
        Matcher m = expression.matcher(s);
        Map<String, Integer> map = new TreeMap<String, Integer>();
        String word = "";
        int times = 0;
        while (m.find()) {// 是否匹配单词
            word = m.group();// 得到一个单词-树映射的键
            if (map.containsKey(word)) {// 如果包含该键，单词出现过
                times = map.get(word);// 得到单词出现的次数
                map.put(word, times + 1);
            } else {
                map.put(word, 1);// 否则单词第一次出现，添加到映射中
            }
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {// 排序,打印
            public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
                return (left.getValue()).compareTo(right.getValue());
            }
        });
        int last = list.size() - 1;
        System.out.println("带停止表的单词：");
        for (int i = last; i > 0; i--) {
            String key = list.get(i).getKey();
            Integer value = list.get(i).getValue();
            System.out.println(key + " :" + value);
        }


    }

        public static void testNumber()
        {
           char o='A';
            try {
                char shuZi[] = new char[1000000];
                char ziMu[] = new char[52];
                int j = 0,k=0;
                int count[] = new int[52];
                String pathName = "src/rersource/a.txt";
                File fileName = new File(pathName);
                InputStreamReader reader = new InputStreamReader(new FileInputStream(fileName));
                BufferedReader br = new BufferedReader(reader);
                String line[] = new String[100];
                for (int i = 0; i < line.length; i++)
                {
                    line[i] = br.readLine();
//                    System.out.println(line[i]);
                }
                br.close();
                while (line[k] != null) {
                    for (int i = 0; i < line[k].length(); i++) {
                        shuZi[j] = line[k].charAt(i);
                        j++;
                    }
                    k++;
                }
                for(int i=0;i<52;i++){
                    ziMu[i]=o;
                    if(o=='Z')
                    o+=7;
                    else o++;
                }
//                     匹配表
                for (int i = 0; i < shuZi.length; i++) {
                    switch (shuZi[i]) {
                        case 'a':
                            count[0]++;
                            break;
                        case 'b':
                            count[1]++;
                            break;
                        case 'c':
                            count[2]++;
                            break;
                        case 'd':
                            count[3]++;
                            break;
                        case 'e':
                            count[4]++;
                            break;
                        case 'f':
                            count[5]++;
                            break;
                        case 'g':
                            count[6]++;
                            break;
                        case 'h':
                            count[7]++;
                            break;
                        case 'i':
                            count[8]++;
                            break;
                        case 'j':
                            count[9]++;
                            break;
                        case 'k':
                            count[10]++;
                            break;
                        case 'l':
                            count[11]++;
                            break;
                        case 'm':
                            count[12]++;
                            break;
                        case 'n':
                            count[13]++;
                            break;
                        case 'o':
                            count[14]++;
                            break;
                        case 'p':
                            count[15]++;
                            break;
                        case 'q':
                            count[16]++;
                            break;
                        case 'r':
                            count[17]++;
                            break;
                        case 's':
                            count[18]++;
                            break;
                        case 't':
                            count[19]++;
                            break;
                        case 'u':
                            count[20]++;
                            break;
                        case 'v':
                            count[21]++;
                            break;
                        case 'w':
                            count[22]++;
                            break;
                        case 'x':
                            count[23]++;
                            break;
                        case 'y':
                            count[24]++;
                            break;
                        case 'z':
                            count[25]++;
                            break;
                        case 'A':
                            count[26]++;
                            break;
                        case 'B':
                            count[27]++;
                            break;
                        case 'C':
                            count[28]++;
                            break;
                        case 'D':
                            count[29]++;
                            break;
                        case 'E':
                            count[30]++;
                            break;
                        case 'F':
                            count[31]++;
                            break;
                        case 'G':
                            count[32]++;
                            break;
                        case 'H':
                            count[33]++;
                            break;
                        case 'I':
                            count[34]++;
                            break;
                        case 'J':
                            count[35]++;
                            break;
                        case 'K':
                            count[36]++;
                            break;
                        case 'L':
                            count[37]++;
                            break;
                        case 'M':
                            count[38]++;
                            break;
                        case 'N':
                            count[39]++;
                            break;
                        case 'O':
                            count[40]++;
                            break;
                        case 'P':
                            count[41]++;
                            break;
                        case 'Q':
                            count[42]++;
                            break;
                        case 'R':
                            count[43]++;
                            break;
                        case 'S':
                            count[44]++;
                            break;
                        case 'T':
                            count[45]++;
                            break;
                        case 'U':
                            count[46]++;
                            break;
                        case 'V':
                            count[47]++;
                            break;
                        case 'W':
                            count[48]++;
                            break;
                        case 'X':
                            count[49]++;
                            break;
                        case 'Y':
                            count[50]++;
                            break;
                        case 'Z':
                            count[51]++;
                    }
                }
                int sum = 0;
                System.out.println("短文中各字母出现情况统计如下：");
                for (int i = 0; i < 52; i++) {
                    if (count[i] != 0) {
                        sum += count[i];
                    }
                }
                int length=count.length;
                for(int i=0; i<length-1; i++){
                    int minIndex = i;
                    for(int p=minIndex+1;p<length;p++){
                        if(count[p]>count[minIndex]){
                            minIndex = p;
                        }else if(count[p]==count[minIndex]){
                            if(ziMu[p]<ziMu[minIndex]){
                                minIndex=p;
                            }
                        }
                    }
                    char temp0=ziMu[i];
                    int temp = count[i];
                    ziMu[i]=ziMu[minIndex];
                    count[i] = count[minIndex];
                    ziMu[minIndex]=temp0;
                    count[minIndex] = temp;
                }
                for (int i = 0; i < 52; i++) {

                        System.out.println(i + ".字母" +"字母"+ziMu[i]+"出现的次数是："+count[i]+"次");
                        double a = (double) ((Math.round(count[i] * 100) / 100.0) / sum) * 100;
                        double b = (double) (Math.round(a * 100) / 100.0);
                        System.out.println( ziMu[i] + "的出现频率是:" + b+"%");
                }
                System.out.println("字母共计：" + sum + "个");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    public static void testWord() throws IOException
    {
        BufferedReader br = new BufferedReader(new FileReader("src/rersource/a.txt"));

        StringBuffer sb = new StringBuffer();
        String text = null;
        while ((text = br.readLine()) != null) {
            sb.append(text);// 将读取出的字符追加到stringbuffer中
        }
        br.close(); // 关闭读入流

        String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
        String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割，得到所有单词
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String word : words) {
            if (map.get(word) == null) { // 若不存在说明是第一次，则加入到map,出现次数为1
                map.put(word, 1);
            } else {
                map.put(word, map.get(word) + 1); // 若存在，次数累加1
            }
        }

        // 排序
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

        Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
                return (left.getValue().compareTo(right.getValue()));
            }
        };
        // 集合默认升序升序
        Collections.sort(list, comparator);
            System.out.println("单词排序：");
            for (int i = 0; i < list.size(); i++) {// 由高到低输出
                System.out.println(list.get(list.size() - i - 1).getKey() + ":" + list.get(list.size() - i - 1).getValue());


        }
    }
    public class piao2 {
        public class stopwords {
            HashSet<String> set = new HashSet<>();

            public stopwords() {
                String File = "";
                try {
                    String path = "src/rersource/b.txt";
                    Scanner in = null;
                    piao2 pp2 = new piao2();
                    in = new Scanner(pp2.get(path));
                    while (in.hasNextLine()) {
                        File += in.nextLine();
                    }
                } catch (Exception e) {
                    System.err.println("无该文件");
                    e.printStackTrace();
                }
                for (String string : File.split(" ")) {
                    set.add(string);
                }
                System.out.println(set);
            }

            public boolean get(String a) {
                return set.contains(a);
            }
        }
        public Readable get(String path){
            // TODO Auto-generated method stub
            return null;
        }


    }

    }
